package com.lyhdev.android.touchenglish;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import android.os.Environment;

public class TEQuiz {
	private String text;
	private String sound;
	private File soundFile = null;
	private String image;
	private File imageFile = null;
	private ArrayList<String> options;
	private ArrayList<Integer> answers;
	private boolean imageCached = false;
	private boolean soundCached = false;

	public synchronized boolean cacheImage() {
		boolean result = true;

		try {
			if (imageFile == null) {
				imageFile = httpload(image, ".jpg");
			}
		} catch (Exception ex) {
			result = false;
			ex.printStackTrace();
		}
		imageCached = result;
		return result;
	}

	public synchronized boolean cacheSound() {
		boolean result = true;

		try {
			if (soundFile == null) {
				soundFile = httpload(sound, ".mp3");
			}
		} catch (Exception ex) {
			result = false;
			ex.printStackTrace();
		}
		soundCached = result;
		return result;
	}

	public boolean isImageCached() {
		return imageCached;
	}

	public boolean isSoundCached() {
		return soundCached;
	}

	private File httpload(String urlPath, String suffix) throws Exception {
		URL url = new URL(urlPath);
		URLConnection conn = url.openConnection();
		conn.connect();
		InputStream is = conn.getInputStream();

		if (is == null) {
			throw new RuntimeException("stream is null");
		}

		File cacheDir = new File(getSDCardPath(), "TouchEnglishCache");
		cacheDir.mkdirs();

		File tempFile = File.createTempFile("temp", suffix, cacheDir);
		FileOutputStream fos = new FileOutputStream(tempFile);

		byte buf[] = new byte[128];
		do {
			int numread = is.read(buf);
			if (numread <= 0) {
				break;
			}
			fos.write(buf, 0, numread);
		} while (true);

		is.close();
		fos.close();

		return tempFile;
	}

	private static File getSDCardPath() {
		return Environment.getExternalStorageDirectory();
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getSound() {
		return sound;
	}

	public void setSound(String sound) {
		this.sound = sound;
	}

	public File getSoundFile() {
		return soundFile;
	}

	public void setSoundFile(File soundFile) {
		this.soundFile = soundFile;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public File getImageFile() {
		return imageFile;
	}

	public void setImageFile(File imageFile) {
		this.imageFile = imageFile;
	}

	public void parseOptions(String opts) {
		options = new ArrayList<String>();
		answers = new ArrayList<Integer>();

		int c = 0;
		for (String option : opts.split("\n")) {
			option = option.trim();

			if (option.startsWith(">>")) {
				options.add(option.replace(">>", "").trim());
				answers.add(c);
			} else {
				options.add(option);
			}

			c++;
		}
	}

	public ArrayList<String> getOptions() {
		return options;
	}

	public ArrayList<Integer> getAnswers() {
		return answers;
	}
}
